﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Interop;

namespace CommonLibrary
{
    public class WindowsScreenHelper
    {
        public double[] GetDPIV1()
        {
            // 需要窗口句柄
            //Graphics currentGraphics = Graphics.FromHwnd(new WindowInteropHelper(mainWindow).Handle);
            Window mainForm = new Window();
            Graphics currentGraphics = Graphics.FromHwnd(new WindowInteropHelper(mainForm).Handle);
            // 除于96，等于缩放比
            double dpixRatio = currentGraphics.DpiX;
            return new double[] { dpixRatio, dpixRatio };
        }

        public double[] GetDPIV2()
        {
            // UNDONE 目前获得的值不对 
            using (ManagementClass mc = new ManagementClass("Win32_DesktopMonitor"))
            {
                using (ManagementObjectCollection moc = mc.GetInstances())
                {

                    int PixelsPerXLogicalInch = 0; // dpi for x
                    int PixelsPerYLogicalInch = 0; // dpi for y

                    foreach (ManagementObject each in moc)
                    {
                        PixelsPerXLogicalInch = int.Parse((each.Properties["PixelsPerXLogicalInch"].Value.ToString()));
                        PixelsPerYLogicalInch = int.Parse((each.Properties["PixelsPerYLogicalInch"].Value.ToString()));
                    }
                    return new double[] { PixelsPerXLogicalInch, PixelsPerYLogicalInch };
                }
            }
        }
    }
}
